home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: reversing a string
- Date: Wed, 10 Apr 96 00:46:15 GMT
- Organization: none
- Message-ID: <829097175snz@genesis.demon.co.uk>
- References: <4k6cjl$j8f@central.server.swt.edu> <4kb1s7$6eu@ibm32.perftech.com>,<829058514snz@genesis.demon.co.uk> <4ke7a6$49m@central.server.swt.edu>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4ke7a6$49m@central.server.swt.edu>
- ln16674@nyssa.swt.edu "Leland Newsom" writes:
-
- >Yes, I was doing that. He said that it could be done without any extra
- >variables.
-
- OK, here's one way:
-
-
- #include <stdio.h>
-
- static void reverse(char *str);
-
- int main(int argc, char *argv[])
-
- {
- if (argc >= 2)
- reverse(argv[1]);
-
- printf("'%s'\n", argv[1]);
-
- return 0;
- }
-
- static void reverse(char *str)
-
- {
- if (str[0] != '\0' && str[1] != '\0') {
- reverse(str+1);
- reverse(str+2);
- str[0] ^= str[1], str[1] ^= str[0], str[0] ^= str[1];
- reverse(str+1);
- }
- }
-
- > He also said that if you get real cryptic you can do the function
- >in one or two lines.
-
- If you wanted to you could code the body of reverse() above as a single
- expression. Most newlines in a C source file are simply white-space so
- you could put the whole function on one line if you wanted to, but it
- doesn't prove very much.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-